package www.wen.com.common.lib.extension

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import com.gyf.immersionbar.ImmersionBar
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.luck.pictureselector.GlideEngine
import www.wen.com.common.R
import www.wen.com.common.lib.helper.OnCameraCancelFunction
import www.wen.com.common.lib.helper.OnCameraResultFunction


/**
 * @author WEN
 * @Description:
 * @date 2020/12/6 17:08
 */
fun Fragment.finishMyself() {
    requireActivity().finish()
}

fun Fragment.navigateNavHostAgency(
        @IdRes resId: Int,
        args: Bundle? = null,
) {
    NavHostFragment
            .findNavController(this)
            .navigate(resId, args)
}

fun Fragment.navigateNavHostAgency(
        view: View,
        @IdRes resId: Int,
        args: Bundle? = null,
) {
    Navigation
            .findNavController(view)
            .navigate(resId, args)
}

fun Fragment.startActivityAgency(cls: Class<*>, bundle: Bundle? = null) {
    requireActivity().run {
        Intent(this, cls).run {
            bundle?.run {
                putExtras(this)
            }
            startActivity(this)
        }
    }
}

fun Fragment.initImmersion(
        isEnable: Boolean = true,
        @ColorRes statusBarColor: Int = R.color.colorFFFFFF,
        view: View? = null,
        autoDarkModeAlpha: Float = 0.2f,
        fits: Boolean = false,
) {
    ImmersionBar
            .with(this)
            .fitsSystemWindows(fits)
            .titleBar(view, true)
            .statusBarColor(statusBarColor) //这里设置状态栏颜色默认白色,透明可设置Color.TRANSPARENT
            .autoDarkModeEnable(isEnable, autoDarkModeAlpha) // 自动根据StatusBar颜色调整深色模式与亮色模式，默认false
            .init() //初始化
}

/**
 * 启动相机
 */
fun Fragment.openCameraAgency(
        onCameraCancelFunction: OnCameraCancelFunction? = null,
        onCameraResultFunction: OnCameraResultFunction? = null,
) {
    PictureSelector.create(this)
            .openCamera(PictureMimeType.ofImage())
            .minimumCompressSize(500)
            .imageEngine(GlideEngine.createGlideEngine())
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: List<LocalMedia>) {
                    onCameraResultFunction?.invoke(result)
                }

                override fun onCancel() {
                    onCameraCancelFunction?.invoke()
                }
            })
}


/**
 * 启动相册
 */
fun Fragment.openPhotoAlbumAgency(
        maxSelectNum: Int = 1,
        size: Int = 200,
        isCompress: Boolean = true,
        enableCrop: Boolean = true,
        aspect_ratio_x: Int = 1,
        aspect_ratio_y: Int = 1,
        onCameraCancelFunction: OnCameraCancelFunction? = null,
        onCameraResultFunction: OnCameraResultFunction? = null,
) {
    PictureSelector.create(this)
            .openGallery(PictureMimeType.ofImage())
            .maxSelectNum(maxSelectNum)
            .isCompress(isCompress)
            .minimumCompressSize(size)
            .isEnableCrop(enableCrop)
            .withAspectRatio(aspect_ratio_x, aspect_ratio_y)
            .imageEngine(GlideEngine.createGlideEngine())
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: List<LocalMedia>) {
                    onCameraResultFunction?.invoke(result)
                }

                override fun onCancel() {
                    onCameraCancelFunction?.invoke()
                }
            })
}
